package com.gcb.controller.operate;

import com.gcb.common.annotation.Log;
import com.gcb.common.constant.UserConstants;
import com.gcb.controller.BaseController;
import com.gcb.common.core.domain.AjaxResult;
import com.gcb.common.core.domain.entity.SysDept;
import com.gcb.common.core.domain.entity.SysUser;
import com.gcb.common.core.page.TableDataInfo;
import com.gcb.common.enums.BusinessType;
import com.gcb.framework.utils.SecurityUtils;
import com.gcb.common.utils.StringUtils;
import com.gcb.common.utils.poi.ExcelUtil;
import com.gcb.framework.domain.dto.OperateInsertDto;
import com.gcb.framework.domain.dto.OperateSearchDto;
import com.gcb.framework.domain.export.OperateExport;
import com.gcb.framework.service.IOperateManageService;
import com.gcb.framework.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.gcb.common.enums.DeptType.OPERATE;


/**
 * 运营管理
 *
 * @author jy
 */
@RestController
@RequestMapping("/operate/manage")
public class OperateController extends BaseController {
    @Autowired
    private IOperateManageService operateManageService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 获取子运营列表
     */
    @PreAuthorize("@ss.hasPermi('operate:manage:list')")
    @GetMapping("/list")
    public TableDataInfo list(OperateSearchDto dept) {
        startPage();
        // 只查询子运营商
        dept.setDeptType(OPERATE.getCode());
        List<SysDept> depts = operateManageService.selectOperatetList(dept);
        return getDataTable(depts);
    }

    /**
     * 导出子运营列表
     */
    @PreAuthorize("@ss.hasPermi('operate:manage:export')")
    @Log(title = "导出子运营列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperateSearchDto dept) {
        // 只查询子运营商
        dept.setDeptType(OPERATE.getCode());
        List<OperateExport> list = operateManageService.selectOperatetExportList(dept);
        ExcelUtil<OperateExport> util = new ExcelUtil<OperateExport>(OperateExport.class);
        util.exportExcel(response, list, "导出子运营列表");
    }

    /**
     * 新增子监管运营
     */
    @PreAuthorize("@ss.hasPermi('operate:manage:add')")
    @Log(title = "监管运营管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody OperateInsertDto dept) {
        if (!deptService.checkDeptNameUnique(dept)) {
            return error("新增子监管运营'" + dept.getDeptName() + "'失败，子监管运营名称已存在");
        }
        dept.setCreateBy(getUsername());
        return toAjax(operateManageService.insertOperate(dept));
    }

    /**
     * 更新运营状态
     */
    @PreAuthorize("@ss.hasPermi('operate:manage:list')")
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody OperateInsertDto dept) {
        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(operateManageService.updateOperateStatus(dept));
    }

    /**
     * 根据部门编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('operate:manage:query')")
    @GetMapping(value = "/getInfo/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId) {
        deptService.checkDeptDataScope(deptId);
        return success(operateManageService.selectOperateById(deptId));
    }

    /**
     * 修改部门
     */
    @PreAuthorize("@ss.hasPermi('operate:manage:edit')")
    @Log(title = "运营管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody OperateInsertDto dept) {
        Long deptId = dept.getDeptId();
        deptService.checkDeptDataScope(deptId);
        if (!deptService.checkDeptNameUnique(dept)) {
            return error("修改运营'" + dept.getDeptName() + "'失败，运营名称已存在");
        } else if (dept.getParentId().equals(deptId)) {
            return error("修改运营'" + dept.getDeptName() + "'失败，上级运营不能是自己");
        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) {
            return error("该运营包含未停用的子部门！");
        }
        dept.setUpdateBy(getUsername());
        dept.setUpdateTime(new Date());
        return toAjax(operateManageService.updateOperate(dept));
    }


    /**
     * 获取运营账号用户列表
     */
    @PreAuthorize("@ss.hasPermi('operate:manage:userList')")
    @GetMapping("/operateUserList")
    public TableDataInfo operateUserList(SysUser user) {
        startPage();
        List<SysUser> list = operateManageService.operateUserList(user);
        return getDataTable(list);
    }
}
